package JavaStudy.SerializableTest;

import java.io.*;

public class TransientTest {
    private static final String url="G://CODE//software//GitHub//repository//1//leetcode//src//SerializableTest//data.txt";

    public static void main(String[] args) {
        try {
            SerializeUser();
            DeSerializeUser();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //序列化
    private static void SerializeUser() throws IOException{
        User user = new User();
        user.setUsername("user1");
        user.setPassword("123456");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(url));
        oos.writeObject(user);
        oos.close();
        System.out.println("普通字段序列化：username=  "+user.getUsername());
        System.out.println("添加了transient关键字序列化：password=  "+user.getPassword());
    }

    //反序列化
    private static void DeSerializeUser() throws IOException, ClassNotFoundException {
        File file = new File(url);
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        User user = (User)ois.readObject();
        System.out.println("普通字段反序列化：username=  "+user.getUsername());
        System.out.println("添加了transient关键字反序列化：password=  "+user.getPassword());
    }
}
